/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-2-6
 * Time: 下午3:28
 * To change this template use File | Settings | File Templates.
 */
/*
 * Copyright (c) 2009, Sina Inc. All rights reserved.
 */
/**
 * @fileoverview boot runner
 * 		load js files by version
 * @author stan | chaoliang@staff.sina.com.cn
 */
(function (){
    //- debug -
    var __debug_mode = false; //should be false
    //- sina lib version - TODO
    var __sina_ver = "0.0.1";

    // js base domain
    var __js_domain = "http://sjs.sinajs.cn/";

    /**
     * @desc add DOM onload Event
     */

    var __addDOMLoadEvent = function(func){
        var __load_events;
        var __load_timer;
//		var binded = false;

        if (!__load_events) {
            var init = function(){
                // quit if this function has already been called
                if (arguments.callee.done) {
                    return;
                }
                // flag this function so we don't do the same thing twice
                arguments.callee.done = true;
                // kill the timer
                if (__load_timer) {
                    clearInterval(__load_timer);
                    __load_timer = null;
                }
                // execute each function in the stack in the order they were added
                for (var i = 0; i < __load_events.length; i++) {
                    __load_events[i]();
                }
                __load_events = null;
            };
            // for Mozilla/Opera9
            if (document.addEventListener) {
                document.addEventListener("DOMContentLoaded", init, false);
//				binded = true;
            }
            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
             var domlen = document.getElementsByTagName("*").length;
             var domnum = 0;
             (function () {
             if(domnum != domlen) {
             setTimeout(arguments.callee, 500);
             }
             else {
             setTimeout(init, 500);
             }
             domnum = domlen;
             })();
             @end @*/
            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                __load_timer = setInterval(function(){
                    if (/loaded|complete/.test(document.readyState)) {
                        init(); // call the onload handler
//	                    binded = true;
                    }
                }, 10);
            }
            // for other browsers
//			if(binded == false){
            window.onload = init;
//			}
            // create event function stack
            __load_events = [];
        }
        // add function to event stack
        __load_events.push(func);
    };

    // 绑定事件
    var addEvent = function(elm, func, evType, useCapture) {
        var _el = $E(elm);
        if(_el == null){ return; }
        if(typeof useCapture == "undefined"){
            useCapture = false;
        }
        if(typeof evType == "undefined"){
            evType = "click";
        }
        if(_el.addEventListener){
            _el.addEventListener(evType, func, useCapture);
            return true;
        }else if(_el.attachEvent){
            var r = _el.attachEvent("on" + evType, func);
            return true;
        }else{
            _el['on' + evType] = func;
        }
    };

    var __addFocusEvent = function(fFocusFunction){
        //TODO not available during the dev
        if(window.trigger != null && typeof window.trigger == "function"){
            trigger();
            trigger = null;
        }
    };

    if(__debug_mode){
        document.write('<script type="text/javascript" src="' + __js_domain + 'bind2/debug/debug_base.js"></script>');
        document.write('<script type="text/javascript" src="' + __js_domain + 'bind2/debug/debug.js"></script>');
    }

    window.__load_js = function(){
        var url = "";
        if(__debug_mode == true){
            url = 	__js_domain + "bind2/index.php?product=" + scope.$PRODUCT_NAME + "&pageid=" + scope.$pageid
                + "&rnd=" + new Date().getTime();
            document.write('<script type="text/javascript" src="' + url + '" charset="utf-8"></script>');
        }else{
            // 取得 JS 版本号
            var jsver =	(typeof $_GLOBAL != "undefined" && $_GLOBAL.ver != null)
                ? $_GLOBAL.ver[scope.$PRODUCT_NAME] || "-1"
                : "-1";
            url = 	__js_domain + scope.$PRODUCT_NAME + "/" + scope.$pageid + ".js?"
                + jsver + ".js";
            document.write('<script type="text/javascript" src="' + url + '" charset="utf-8"></script>');
        }
    };

    window.__render_page = function(){
        __addDOMLoadEvent(main);
        addEvent(document.body, __addFocusEvent, "focus");
        addEvent(window, __addFocusEvent, "scroll");
        addEvent(document.body, __addFocusEvent, "mousemove");
        addEvent(document.body, __addFocusEvent, "mouseover");
    };

    if (typeof scope.$setDomain == "undefined" || scope.$setDomain == true) {
        document.domain = "sina.com.cn";
    }
})();